package com.tajiang.business.businesscount.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.tajiang.business.R;
import com.tajiang.business.common.widget.WheelView;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * 通用对话框
 *
 * @author yxx
 *
 * @date 2016-5-3 下午10:00:31
 *
 */
public class StoreOpenTimeDialog {

	public static boolean isShow = false;
	static Dialog dialog;

	public static WheelView wheelViewHour;
	public static WheelView wheelViewMin;

	// 弹窗结果回调函数
	public static abstract class DialogCallback {
		public abstract void callback(int position, String hour, String minute);
	}

	public StoreOpenTimeDialog(Context context){

	}

	/**
	 * @param context
	 */
	public static void showDialog(Context context, final DialogCallback callback){

		if(dialog != null){
			dialog.dismiss();
		}

		dialog = new Dialog(context, R.style.dialog);
		LayoutInflater inflater = dialog.getLayoutInflater();
		View layout = inflater.inflate(R.layout.dialog_store_open_time, null);

		wheelViewHour = (WheelView) layout.findViewById(R.id.dialog_store_open_time_hour);
		wheelViewMin = (WheelView) layout.findViewById(R.id.dialog_store_open_time_minute);

		initTimeData();

		wheelViewHour.setOnWheelViewListener(new WheelView.OnWheelViewListener() {

			@Override
			public void onSelected(int selectedIndex, String item) {

			}
		});

		TextView tvCancel = (TextView) layout.findViewById(R.id.dialog_store_open_cancel);
		tvCancel.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				closeDialog();
			}
		});

		TextView tvOk = (TextView) layout.findViewById(R.id.dialog_store_open_ok);
		tvOk.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {

				closeDialog();
				callback.callback(0, wheelViewHour.getSeletedItem(), wheelViewMin.getSeletedItem());//
			}
		});

		TextView tvStop = (TextView) layout.findViewById(R.id.dialog_store_open_stop);
		tvStop.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {

				closeDialog();
				callback.callback(1, "", "");//
			}
		});

		dialog.setContentView(layout);

		dialog.setCancelable(true);
		dialog.setCanceledOnTouchOutside(false);

		dialog.show();
		setDialogWindowAttr(dialog, context);
		isShow = true;
	}

	public static void initTimeData(){

		List<String> listHour = new ArrayList<String>();
		List<String> listMin = new ArrayList<String>();

		for(int i=0; i<24; i++){
			listHour.add(i+"");
		}

		for(int i=0; i<12; i++){
			listMin.add(i * 5 + "");
		}

		wheelViewHour.setItems(listHour);
		wheelViewMin.setItems(listMin);

		Calendar c = Calendar.getInstance();
		int hour = c.get(Calendar.HOUR_OF_DAY);
		int minute = c.get(Calendar.MINUTE);

		int hourPos = hour;
		int minPos = new Double(minute/5).intValue() + 1;
		if(minPos >= 11){
			minPos = 0;
			++hourPos;
		}

		if(hourPos >= 23){
			hourPos = 0;
		}

		wheelViewHour.setSeletion(hourPos);
		wheelViewMin.setSeletion(minPos);
	}

	/**
	 * 关闭窗口
	 */
	public static void closeDialog(){

		if(dialog != null){
			dialog.dismiss();
		}
	}

	//在dialog.show()之后调用
	public static void setDialogWindowAttr(Dialog dlg,Context ctx){

		WindowManager wm = ((Activity) ctx).getWindowManager();
		Display display = wm.getDefaultDisplay(); // 为获取屏幕宽、高

		Window window = dlg.getWindow();
		WindowManager.LayoutParams lp = window.getAttributes();
		lp.gravity = Gravity.CENTER;
		lp.width = (int) (display.getWidth()/1.2);
		lp.height = (int) (display.getHeight()/1.5);
		dlg.getWindow().setAttributes(lp);
	}

}
